package edu.calvin.djs32.cs214.interpreters.prettyprinter;

import edu.calvin.cs214.hobbes2008.ApplicationTIR;
import edu.calvin.cs214.hobbes2008.HobbesTIR;
import edu.calvin.cs214.hobbes2008.HobbesTIRVisitor;
import edu.calvin.cs214.hobbes2008.IdentifierTIR;
import edu.calvin.cs214.hobbes2008.IfTIR;
import edu.calvin.cs214.hobbes2008.IntegerTIR;
import edu.calvin.cs214.hobbes2008.LetFunctionTIR;
import edu.calvin.cs214.hobbes2008.LetTIR;
import edu.calvin.cs214.hobbes2008.StringTIR;
import edu.calvin.cs214.hobbes2008.TupleTIR;

	
public class PrettyPrinter implements HobbesTIRVisitor<String>{
	public IHTMLGenerator myGenerator;
	public PrettyPrinter(IHTMLGenerator generator){
		 myGenerator = generator;
	}
	
	private String computeCssClass(int i) {
		return ((i >= 0) ? "integer" : "integer negative");
	}

	public String interpret(HobbesTIR tir) {
		return myGenerator.pre(tir.accept(this));
	}

	public String visitInteger(IntegerTIR integerTIR) {
		int i = integerTIR.getInt();
		return myGenerator.span(integerTIR.toString(), computeCssClass(i));
	}

	public String visitApplication(ApplicationTIR application) {
		TupleTIR tTir = application.getTuple();
		String tReturn = tTir.accept(this);
		String idReturn = myGenerator.span(application.getIdentifier().toString(), "identifier");
		
		
		return myGenerator.span(idReturn + tReturn,"application");
	}

	public String visitString(StringTIR string) {
		return myGenerator.span("&lsquo;" + string + "&rsquo;", "string");
	}

	public String visitTuple(TupleTIR tuple) {
		String tupleReturn = "";
		for (int i = 0; i < tuple.getLength(); i++) {
			tupleReturn += tuple.getElement(i).accept(this);
			if (i == (tuple.getLength()-1) ){
				
			}else{
				tupleReturn += ", ";
			}
		}
		return myGenerator.span("(" + tupleReturn + ")", "tuple");
	}

	public String visitIf(IfTIR ife) {
		String sCond = ife.getCondition().accept(this);
		String sThen = ife.getThen().accept(this);
		String sElse = ife.getElse().accept(this);
		return myGenerator.span("if " + sCond + " then " + sThen + " else " + sElse + " end" , "if");
	}

	public String visitIdentifier(IdentifierTIR id) {
		return myGenerator.span(id.getIdentifier(), "identifier");
	}

	public String visitLet(LetTIR letTIR) {
		String sIdent = letTIR.getIdentifier().accept(this);
		String sInit = letTIR.getInitialization().accept(this);
		String sBody = letTIR.getBody().accept(this);
		return myGenerator.span("let " + sIdent + " := " + sInit + " in " + sBody, "let");
	}

	public String visitLetFunction(LetFunctionTIR let) {
		// TODO Auto-generated method stub
		return null;
	}

}
